package org.fireflow2.designer.eclipse.parts.basic;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.draw2d.Bendpoint;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;
import org.fireflow2.designer.eclipse.figures.TransitionFigure;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.TransitionWrapper;

public class BasicTransitionPart extends AbstractConnectionEditPart implements
		PropertyChangeListener {

	public BasicTransitionPart() {

	}

	public void activate() {
		if (isActive()) {
			return;
		}
		super.activate();
		((AbsWrapper) getModel()).addPropertyChangeListener(this);
	}

	public void deactivate() {
		if (!isActive()) {
			return;
		}
		super.deactivate();
		((AbsWrapper) getModel()).removePropertyChangeListener(this);
	}

	@Override
	protected IFigure createFigure() {

		TransitionFigure transitionFigure = new TransitionFigure(
				((TransitionWrapper) getModel()));

		return transitionFigure;
	}

	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
				new ConnectionEndpointEditPolicy());
	}

	protected void refreshVisuals() {
		List<Bendpoint> constraints = ((TransitionWrapper) getModel())
				.getConstraints();
		((PolylineConnection) getFigure()).setRoutingConstraint(constraints);

		((TransitionFigure) this.getFigure()).refreshFigure();
	}

	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
		// refreshVisuals();
		if (evt.getPropertyName().equals(AbsWrapper.BENDPOINT)
				|| evt.getPropertyName().equals(AbsWrapper.NAME)
				|| evt.getPropertyName().equals(AbsWrapper.DISPLAYNAME)
				|| evt.getPropertyName().equals(AbsWrapper.LABEL_POSITION)) {
			refreshVisuals();
		}

	}

}
